﻿using System.Collections.Generic;
using System.Linq;
using DotNet.Configuration;
using DotNet.Helper;

namespace TimeCard.Core
{
    public class RuleService
    {
        private static readonly List<UserRule> Rules;

        static RuleService()
        {
            Rules = JsonHelper.Deserialize(GetPath(), new List<UserRule>());
        }

        //public bool Exists(UserRule entity)
        //{
        //    if (Rules.Any(p => p.StartHour == entity.StartHour && p.StartMinute == entity.StartMinute
        //            && p.EndHour == entity.EndHour && p.EndMinute == entity.EndMinute))
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        public bool Exists(UserRule entity)
        {
            var model = Rules.FirstOrDefault(p => p.StartHour == entity.StartHour && p.StartMinute == entity.StartMinute
                                                  && p.EndHour == entity.EndHour && p.EndMinute == entity.EndMinute);
            if (model == null || model.Id == entity.Id)
            {
                return false;
            }
            return true;
        }

        public void Add(UserRule entity)
        {
            var model = new UserRule();
            model.Id = GetMaxId();
            model.StartHour = entity.StartHour;
            model.StartMinute = entity.StartMinute;
            model.EndHour = entity.EndHour;
            model.EndMinute = entity.EndMinute;
            model.HasWeekend = entity.HasWeekend;
            model.Users = entity.Users;
            Rules.Add(model);
            Save();
        }

        private int GetMaxId()
        {
            if (Rules.Count == 0)
            {
                return 1;
            }
            return Rules.Max(p => p.Id)+1;
        }

        public void Edit(UserRule entity)
        {
            var model = Rules.FirstOrDefault(p => p.Id == entity.Id);
            if (model == null)
            {
                return;
            }
            model.StartHour = entity.StartHour;
            model.StartMinute = entity.StartMinute;
            model.EndHour = entity.EndHour;
            model.EndMinute = entity.EndMinute;
            model.Users = entity.Users;
            model.HasWeekend = entity.HasWeekend;
            Save();
        }

        public void Delete(int id)
        {
            var index = Rules.FindIndex(p => p.Id == id);
            if (index < 0)
            {
                return;
            }
            Rules.RemoveAt(index);
            Save();
        }

        public UserRule Get(int id)
        {
            var model = Rules.FirstOrDefault(p => p.Id == id) ?? new UserRule();
            return model;
        }

        public List<UserRule> GetList()
        {
            return Rules;
        }


        private void Save()
        {
            var path = GetPath();
            JsonHelper.Serialize(path, Rules);
        }

        private static string GetPath()
        {
            return DirHelper.GetRootFilePath("Data", "Rule.json");
        }
    }
}